package org.pay.manage.merchant.ctrl;

import com.alibaba.fastjson.JSONObject;
import org.pay.manage.merchant.service.MchInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.pay.core.common.constant.Constant;
import org.pay.core.common.constant.MchConstant;
import org.pay.core.common.constant.RetEnum;
import org.pay.core.common.domain.BizResponse;
import org.pay.core.common.domain.PayPageRes;
import org.pay.core.common.domain.PayResponse;
import org.pay.core.entity.MchInfo;
import org.pay.manage.common.ctrl.BaseController;
import org.pay.manage.common.service.RpcCommonService;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/mch_info")
//@PreAuthorize("hasRole('"+ MchConstant.MGR_ROLE_NORMAL+"')")
public class MchInfoController extends BaseController {

    @Autowired
    private MchInfoService mchInfoService;

    @Autowired
    private RpcCommonService rpcCommonService;

    /**
     * 查询商户信息
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public ResponseEntity<?> get(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long mchId = getLongRequired(param, "mchId");
        MchInfo mchInfo = rpcCommonService.rpcMchInfoService.findByMchId(mchId);
        return ResponseEntity.ok(PayResponse.buildSuccess(mchInfo));
    }

    /**
     * 新增商户信息
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public ResponseEntity<?> add(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        MchInfo mchInfo = getObject(param, MchInfo.class);
        // 设置默认登录密码
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String rawPassword = MchConstant.MCH_DEFAULT_PASSWORD;
        mchInfo.setCipher(encoder.encode(rawPassword));
        mchInfo.setRole(MchConstant.MCH_ROLE_NORMAL);
        mchInfo.setLastPasswordResetTime(new Date());
        // 确认手机不能重复
        if(rpcCommonService.rpcMchInfoService.findByMobile(mchInfo.getMobile()) != null) {
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_MCH_MOBILE_USED));
        }
        // 确认邮箱不能重复
        if(rpcCommonService.rpcMchInfoService.findByEmail(mchInfo.getEmail()) != null) {
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_MCH_EMAIL_USED));
        }
        int count = rpcCommonService.rpcMchInfoService.addMchInfo(mchInfo);
        if(count != 1) ResponseEntity.ok(PayResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
        return ResponseEntity.ok(BizResponse.buildSuccess());
    }

    /**
     * 修改商户信息
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResponseEntity<?> update(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long mchId = getLongRequired(param, "mchId");
        MchInfo mchInfo = getObject(param, MchInfo.class);
        MchInfo dbMchInfo = rpcCommonService.rpcMchInfoService.findByMchId(mchId);
        if(dbMchInfo == null) {
            ResponseEntity.ok(PayResponse.build(RetEnum.RET_COMM_RECORD_NOT_EXIST));
        }

        if(mchInfo.getMobile() != null && mchInfo.getMobile().longValue() != dbMchInfo.getMobile().longValue()) {
            // 确认手机不能重复
            if(rpcCommonService.rpcMchInfoService.findByMobile(mchInfo.getMobile()) != null) {
                return ResponseEntity.ok(BizResponse.build(RetEnum.RET_MCH_MOBILE_USED));
            }
        }

        if(mchInfo.getEmail() != null && !mchInfo.getEmail().equalsIgnoreCase(dbMchInfo.getEmail())) {
            // 确认邮箱不能重复
            if(rpcCommonService.rpcMchInfoService.findByEmail(mchInfo.getEmail()) != null) {
                return ResponseEntity.ok(BizResponse.build(RetEnum.RET_MCH_EMAIL_USED));
            }
        }

        // 商户信息只允许修改手机号,邮箱,费率,结算方式,状态,其他不允许修改
        MchInfo updateMchInfo = new MchInfo();
        updateMchInfo.setMchId(mchId);
        updateMchInfo.setMobile(mchInfo.getMobile());
        updateMchInfo.setEmail(mchInfo.getEmail());
        updateMchInfo.setMchRate(mchInfo.getMchRate());
        updateMchInfo.setSettType(mchInfo.getSettType());
        updateMchInfo.setState(mchInfo.getState());
        int count = rpcCommonService.rpcMchInfoService.update(updateMchInfo);
        if(count != 1) ResponseEntity.ok(PayResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
        return ResponseEntity.ok(BizResponse.buildSuccess());
    }

    @RequestMapping("/list")
    @ResponseBody
    public ResponseEntity<?> list(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        MchInfo mchInfo = getObject(param, MchInfo.class);
        int count = mchInfoService.countNormal(mchInfo);
        if(count == 0) ResponseEntity.ok(PayPageRes.buildSuccess());
        List<MchInfo> mchInfoList = mchInfoService.getMchNormalInfoList((getPageIndex(param) - 1) * getPageSize(param), getPageSize(param), mchInfo);
        return ResponseEntity.ok(PayPageRes.buildSuccess(mchInfoList, count));
    }


}